package com.juc.phaser.phaser5;

import java.util.concurrent.Phaser;

public class MyService {
    /**
     * 方法 onAdvance()的作用是通过新的屏障时被调用
     * @Override
			protected boolean onAdvance(int phase, int registeredParties) {
                 System.out.println("protected boolean onAdvance(int phase, int registeredParties) 被调用 ！");
				return true;
				//返回true 不等待了 Phaser 呈无效/销毁 的状态
				//返回false 则Phaser 继续工作
			}
     */
	
	private Phaser phaser;
	
	public MyService(Phaser phaser) {
		super();
		this.phaser = phaser;
	}
	public void testMethod() {
		try {
			System.out.println("A begin ThreadName= " + Thread.currentThread().getName() + "         " + System.currentTimeMillis());
			if (Thread.currentThread().getName().equals("B")) {
					Thread.sleep(5000);
			}
			phaser.arriveAndAwaitAdvance();
			System.out.println("A end ThreadName= " + Thread.currentThread().getName() + "     " + " end phase value = " + phaser.getPhase()  + " " +System.currentTimeMillis());
			System.out.println("B begin ThreadName= " + Thread.currentThread().getName() + "         " + System.currentTimeMillis());
			if (Thread.currentThread().getName().equals("B")) {
				Thread.sleep(5000);
		    }
			phaser.arriveAndAwaitAdvance();
			System.out.println("B end ThreadName= " + Thread.currentThread().getName() + "     " + " end phase value = " + phaser.getPhase()  + " " +System.currentTimeMillis());
			System.out.println("C begin ThreadName= " + Thread.currentThread().getName() + "         " + System.currentTimeMillis());
			if (Thread.currentThread().getName().equals("B")) {
				Thread.sleep(5000);
		    }
			phaser.arriveAndAwaitAdvance();
			System.out.println("C end ThreadName= " + Thread.currentThread().getName() + "     " + " end phase value = " + phaser.getPhase()  + " " +System.currentTimeMillis());

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
